﻿/******************************************************************************
文件名称    :   MySqlChemistryBloodDataProvider.cs
创建日期    :   2011/5/13 6:19:49
功能描述    :   ChemistryBlood实体数据提供程序MySql实现类
修改日志    :   
    2011-05-13 06:19  马利
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Dare.Utilities.Data;
using Stone.Data.Providers;
using Stone.Components.Entities;

namespace Stone.Data.MySql.Providers
{
    /// <summary>
    /// ChemistryBlood实体数据提供程序MySql实现类
    /// </summary>
    public class MySqlChemistryBloodDataProvider : ChemistryBloodDataProvider
    {
        public MySqlChemistryBloodDataProvider()
            : base()
        {
        }

        public override void Insert(TransactionManager manager, ChemistryBlood entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_chemistry_blood(wbc, lymL, m0L, granL, e0L, bas0L, lymP, m0P, e0P, bas0P, granP, rbc, hgb, hct, plt, comment) values(?wbc, ?lymL, ?m0L, ?granL, ?e0L, ?bas0L, ?lymP, ?m0P, ?e0P, ?bas0P, ?granP, ?rbc, ?hgb, ?hct, ?plt, ?comment);" +
                    "select id, time_stamp from tb_chemistry_blood where id = last_insert_id();";
                GenericDbHelper.AddParameter(mgr, cmd, "?wbc", DbType.Decimal).Value = entity.Wbc == null ? DBNull.Value : (object)entity.Wbc.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?lymL", DbType.Decimal).Value = entity.LymL == null ? DBNull.Value : (object)entity.LymL.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?m0L", DbType.Decimal).Value = entity.M0L == null ? DBNull.Value : (object)entity.M0L.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?granL", DbType.Decimal).Value = entity.GranL == null ? DBNull.Value : (object)entity.GranL.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?e0L", DbType.Decimal).Value = entity.E0L == null ? DBNull.Value : (object)entity.E0L.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?bas0L", DbType.Decimal).Value = entity.Bas0L == null ? DBNull.Value : (object)entity.Bas0L.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?lymP", DbType.Decimal).Value = entity.LymP == null ? DBNull.Value : (object)entity.LymP.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?m0P", DbType.Decimal).Value = entity.M0P == null ? DBNull.Value : (object)entity.M0P.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?e0P", DbType.Decimal).Value = entity.E0P == null ? DBNull.Value : (object)entity.E0P.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?bas0P", DbType.Decimal).Value = entity.Bas0P == null ? DBNull.Value : (object)entity.Bas0P.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?granP", DbType.Decimal).Value = entity.GranP == null ? DBNull.Value : (object)entity.GranP.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?rbc", DbType.Decimal).Value = entity.Rbc == null ? DBNull.Value : (object)entity.Rbc.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?hgb", DbType.Decimal).Value = entity.Hgb == null ? DBNull.Value : (object)entity.Hgb.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?hct", DbType.Decimal).Value = entity.Hct == null ? DBNull.Value : (object)entity.Hct.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?plt", DbType.Decimal).Value = entity.Plt == null ? DBNull.Value : (object)entity.Plt.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;

                //执行数据库操作,并获得主键和更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.Id = reader.GetInt32(0);
                        entity.TimeStamp = reader.GetDateTime(1);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //插入关联
                InsertRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Import(TransactionManager manager, IEnumerable<ChemistryBlood> entities)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_chemistry_blood(id, wbc, lymL, m0L, granL, e0L, bas0L, lymP, m0P, e0P, bas0P, granP, rbc, hgb, hct, plt, comment, time_stamp) values(?id, ?wbc, ?lymL, ?m0L, ?granL, ?e0L, ?bas0L, ?lymP, ?m0P, ?e0P, ?bas0P, ?granP, ?rbc, ?hgb, ?hct, ?plt, ?comment, ?time_stamp);";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?wbc", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?lymL", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?m0L", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?granL", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?e0L", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?bas0L", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?lymP", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?m0P", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?e0P", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?bas0P", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?granP", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?rbc", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?hgb", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?hct", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?plt", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String);
                GenericDbHelper.AddParameter(mgr, cmd, "?time_stamp", DbType.DateTime);

                //执行数据库操作
                foreach (ChemistryBlood entity in entities)
                {
                    int i = 0;
                    cmd.Parameters[i++].Value = entity.Id;
                    cmd.Parameters[i++].Value = entity.Wbc == null ? DBNull.Value : (object)entity.Wbc.Value;
                    cmd.Parameters[i++].Value = entity.LymL == null ? DBNull.Value : (object)entity.LymL.Value;
                    cmd.Parameters[i++].Value = entity.M0L == null ? DBNull.Value : (object)entity.M0L.Value;
                    cmd.Parameters[i++].Value = entity.GranL == null ? DBNull.Value : (object)entity.GranL.Value;
                    cmd.Parameters[i++].Value = entity.E0L == null ? DBNull.Value : (object)entity.E0L.Value;
                    cmd.Parameters[i++].Value = entity.Bas0L == null ? DBNull.Value : (object)entity.Bas0L.Value;
                    cmd.Parameters[i++].Value = entity.LymP == null ? DBNull.Value : (object)entity.LymP.Value;
                    cmd.Parameters[i++].Value = entity.M0P == null ? DBNull.Value : (object)entity.M0P.Value;
                    cmd.Parameters[i++].Value = entity.E0P == null ? DBNull.Value : (object)entity.E0P.Value;
                    cmd.Parameters[i++].Value = entity.Bas0P == null ? DBNull.Value : (object)entity.Bas0P.Value;
                    cmd.Parameters[i++].Value = entity.GranP == null ? DBNull.Value : (object)entity.GranP.Value;
                    cmd.Parameters[i++].Value = entity.Rbc == null ? DBNull.Value : (object)entity.Rbc.Value;
                    cmd.Parameters[i++].Value = entity.Hgb == null ? DBNull.Value : (object)entity.Hgb.Value;
                    cmd.Parameters[i++].Value = entity.Hct == null ? DBNull.Value : (object)entity.Hct.Value;
                    cmd.Parameters[i++].Value = entity.Plt == null ? DBNull.Value : (object)entity.Plt.Value;
                    cmd.Parameters[i++].Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                    cmd.Parameters[i++].Value = entity.TimeStamp;
                    cmd.ExecuteNonQuery();
                }

                //插入关联
                InsertRelations(mgr, cmd, entities);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Update(TransactionManager manager, ChemistryBlood entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //更新SQL语句
                cmd.CommandText = "update tb_chemistry_blood set wbc = ?wbc, lymL = ?lymL, m0L = ?m0L, granL = ?granL, e0L = ?e0L, bas0L = ?bas0L, lymP = ?lymP, m0P = ?m0P, e0P = ?e0P, bas0P = ?bas0P, granP = ?granP, rbc = ?rbc, hgb = ?hgb, hct = ?hct, plt = ?plt, comment = ?comment where id = ?id;"
                        + "select time_stamp from tb_chemistry_blood where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?wbc", DbType.Decimal).Value = entity.Wbc == null ? DBNull.Value : (object)entity.Wbc.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?lymL", DbType.Decimal).Value = entity.LymL == null ? DBNull.Value : (object)entity.LymL.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?m0L", DbType.Decimal).Value = entity.M0L == null ? DBNull.Value : (object)entity.M0L.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?granL", DbType.Decimal).Value = entity.GranL == null ? DBNull.Value : (object)entity.GranL.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?e0L", DbType.Decimal).Value = entity.E0L == null ? DBNull.Value : (object)entity.E0L.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?bas0L", DbType.Decimal).Value = entity.Bas0L == null ? DBNull.Value : (object)entity.Bas0L.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?lymP", DbType.Decimal).Value = entity.LymP == null ? DBNull.Value : (object)entity.LymP.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?m0P", DbType.Decimal).Value = entity.M0P == null ? DBNull.Value : (object)entity.M0P.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?e0P", DbType.Decimal).Value = entity.E0P == null ? DBNull.Value : (object)entity.E0P.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?bas0P", DbType.Decimal).Value = entity.Bas0P == null ? DBNull.Value : (object)entity.Bas0P.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?granP", DbType.Decimal).Value = entity.GranP == null ? DBNull.Value : (object)entity.GranP.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?rbc", DbType.Decimal).Value = entity.Rbc == null ? DBNull.Value : (object)entity.Rbc.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?hgb", DbType.Decimal).Value = entity.Hgb == null ? DBNull.Value : (object)entity.Hgb.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?hct", DbType.Decimal).Value = entity.Hct == null ? DBNull.Value : (object)entity.Hct.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?plt", DbType.Decimal).Value = entity.Plt == null ? DBNull.Value : (object)entity.Plt.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = entity.Id;

                //执行数据库操作,并获得更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.TimeStamp = reader.GetDateTime(0);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //更新关联
                UpdateRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Delete(TransactionManager manager, long key)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, key);

                //删除SQL语句
                cmd.CommandText = "delete from tb_chemistry_blood where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行
                cmd.ExecuteNonQuery();
                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void DeleteAll(TransactionManager manager, string whereClause)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------具体SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, whereClause);

                //删除SQL语句
                cmd.CommandText = String.Format("delete from tb_chemistry_blood {0}",
                     ProcessWhereClause(whereClause));

                //执行
                cmd.ExecuteNonQuery();

                /*------------------------------具体SQL操作结束----------------------------------*/
            });
        }

        public override ChemistryBlood Get(TransactionManager manager, long key)
        {
            ChemistryBlood entity = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = "select * from tb_chemistry_blood where id = ?id";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行获取实体
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity = Fill(reader, null);
                    }
                }

                //获取关联
                if (entity != null)
                {
                    RetrieveRelations(mgr, cmd, entity);
                    entity.EntityState = Stone.Components.EntityState.Saved;
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return entity;
        }

        public override List<ChemistryBlood> GetPaged(TransactionManager manager, string whereClause, string orderBy, int pageIndex, int pageSize)
        {
            List<ChemistryBlood> list = new List<ChemistryBlood>(pageSize > 0 ? Math.Min(pageSize, 256) : 32);
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select * from tb_chemistry_blood {0} {1} {2}",
                    ProcessWhereClause(whereClause),
                    ProcessOrderBy(orderBy),
                    ProcessPaging(pageIndex, pageSize));

                //执行获取实体集
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(Fill(reader, null));
                    }
                }

                //获取关联
                RetrieveRelations(mgr, cmd, list);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return list;
        }

        public override int GetCount(TransactionManager manager, string whereClause)
        {
            int count = 0;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*) from tb_chemistry_blood {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                count = Convert.ToInt32(cmd.ExecuteScalar());

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return count;
        }

        public override long GetVersion(TransactionManager manager, string whereClause)
        {
            long count = 0;
            DateTime? timeStamp = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*), max(time_stamp) from tb_chemistry_blood {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        count = reader.GetInt64(0);
                        timeStamp = reader.IsDBNull(1) ? (DateTime?)null : reader.GetDateTime(1);
                    }
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return (timeStamp ?? DateTime.MinValue).Ticks ^ count;
        }

        public override ChemistryBlood Fill(IDataReader reader, ChemistryBlood entity)
        {
            if (entity == null) entity = new ChemistryBlood();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                switch (reader.GetName(i))
                {
                    case "id":
                        entity.Id = reader.GetInt64(i);
                        break;
                    case "wbc":
                        entity.Wbc = reader[i] == DBNull.Value ? entity.Wbc : reader.GetDecimal(i);
                        break;
                    case "lymL":
                        entity.LymL = reader[i] == DBNull.Value ? entity.LymL : reader.GetDecimal(i);
                        break;
                    case "m0L":
                        entity.M0L = reader[i] == DBNull.Value ? entity.M0L : reader.GetDecimal(i);
                        break;
                    case "granL":
                        entity.GranL = reader[i] == DBNull.Value ? entity.GranL : reader.GetDecimal(i);
                        break;
                    case "e0L":
                        entity.E0L = reader[i] == DBNull.Value ? entity.E0L : reader.GetDecimal(i);
                        break;
                    case "bas0L":
                        entity.Bas0L = reader[i] == DBNull.Value ? entity.Bas0L : reader.GetDecimal(i);
                        break;
                    case "lymP":
                        entity.LymP = reader[i] == DBNull.Value ? entity.LymP : reader.GetDecimal(i);
                        break;
                    case "m0P":
                        entity.M0P = reader[i] == DBNull.Value ? entity.M0P : reader.GetDecimal(i);
                        break;
                    case "e0P":
                        entity.E0P = reader[i] == DBNull.Value ? entity.E0P : reader.GetDecimal(i);
                        break;
                    case "bas0P":
                        entity.Bas0P = reader[i] == DBNull.Value ? entity.Bas0P : reader.GetDecimal(i);
                        break;
                    case "granP":
                        entity.GranP = reader[i] == DBNull.Value ? entity.GranP : reader.GetDecimal(i);
                        break;
                    case "rbc":
                        entity.Rbc = reader[i] == DBNull.Value ? entity.Rbc : reader.GetDecimal(i);
                        break;
                    case "hgb":
                        entity.Hgb = reader[i] == DBNull.Value ? entity.Hgb : reader.GetDecimal(i);
                        break;
                    case "hct":
                        entity.Hct = reader[i] == DBNull.Value ? entity.Hct : reader.GetDecimal(i);
                        break;
                    case "plt":
                        entity.Plt = reader[i] == DBNull.Value ? entity.Plt : reader.GetDecimal(i);
                        break;
                    case "comment":
                        entity.Comment = reader[i] == DBNull.Value ? entity.Comment : reader.GetString(i);
                        break;
                    case "time_stamp":
                        entity.TimeStamp = reader.GetDateTime(i);
                        break;
                }
            }

            entity.EntityState = Stone.Components.EntityState.Saved;
            return entity;
        }

        #region 实体关联操作数据提供类

        #endregion
    }
}
